VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "IScreen_Title"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'====================================================================================
'IScreen_Title - The game's title screen
'====================================================================================
Option Explicit

'This is a snap-in class for `Project.GameScreen`, shown on screen
Implements IScreen

Private Const MenuTop = 250               'Vertical position of the first menu item
Private Const MenuSpacing = 35            'Space between menu items

'--- Menu Items ---------------------------------------------------------------------
Private MenuItems(3) As New clsButton
Private Enum PM_SELECTEDMENU
    SM_StartGame = 0
    SM_HowToPlay = 1
    SM_HighScores = 2
    SM_Quit = 3
End Enum
Private SelectedMenu As PM_SELECTEDMENU   'Which menu item is selected

'--- Sprites ------------------------------------------------------------------------
Private MarX As Long, MarY As Long        'Mar's location on the screen
Private MarDirection As PM_MAR_ANIM       'Direction Mar travels

Private Type PM_TITLEGHOST
    X As Long
    Y As Long
    Dir As PM_DIRECTION
End Type
Private Ghosts(3) As PM_TITLEGHOST
Private Corners(3) As PM_XY

'--- Graphics -----------------------------------------------------------------------
Private Background As New clsScrollingBG  'The scrolling background

'====================================================================================
'CLASS Initialize: Prepare the Title Screen
'====================================================================================
Private Sub Class_Initialize()
    Dim K As Long
    
    'Setup scrolling background
    Background.Initialize "Candy", -2, 1
    
    'Set the menu items text
    MenuItems(0).Text = "Start Game"
    MenuItems(1).Text = "How To Play"
    MenuItems(2).Text = "High Scores"
    MenuItems(3).Text = "Quit"
    SelectedMenu = SM_StartGame
    
    'Position & prepare the menu items
    For K = 0 To 3
        Set MenuItems(K).Font = Fonts.Menu
        MenuItems(K).CenterX 640
        MenuItems(K).Y = MenuTop + (MenuSpacing * K)
        SharedAPI.SelectObject MenuItems(K).hDC(True), SharedAPI.GetStockObject(BLACK_BRUSH)
    Next K
    
    'Setup the Mar sprite
    MarX = -64                'Hide Mar off the left of the screen
    MarDirection = MA_Right   'Facing right
    
    'Setup the boundaries for the ghosts to follow (Clockwise)
    Corners(0).X = -32: Corners(3).X = Corners(0).X
    Corners(0).Y = 215: Corners(1).Y = Corners(0).Y
    Corners(1).X = 672: Corners(2).X = Corners(1).X
    Corners(2).Y = 415: Corners(3).Y = Corners(2).Y
    
    For K = 0 To 3
        Ghosts(K).Dir = DIR_UP
        Ghosts(K).Y = Corners(0).Y + (K * 70)
        Ghosts(K).X = Corners(0).X
    Next K
        
    'Start Animation
    ScreenMode = SM_Load
End Sub

'====================================================================================
'ISCREEN Animate: Advance one frame on this screen
'====================================================================================
Private Sub IScreen_Animate()
    Dim K As Long
    
    'Which animation mode is the title screen in
    '--- Load -----------------------------------------------------------------------
    If ScreenMode = SM_Load Then
        'When loading the title screen, slide open the doors
        If Status.OpenDoors(STATUS_SPEED_STANDARD) Then ScreenMode = SM_Wait
        
        
    '--- Exit -----------------------------------------------------------------------
    ElseIf ScreenMode = SM_Exit Then
        Select Case ModeStep
            'setup mar on the right side of the screen
            Case 0 '.................................................................
                'Start Mar on the left or right depending on menu chosen
                MarY = (MenuTop + (MenuSpacing * SelectedMenu)) - 22
                If (SelectedMenu / 2) = (SelectedMenu \ 2) Then
                    MarDirection = MA_Right: MarX = -64
                Else
                    MarDirection = MA_Left: MarX = 640
                End If
                ModeStep = 1
            
            'Make Mar sweep across the screen and eat the text
            Case 1 '.................................................................
                If (MarDirection = MA_Right And MarX >= 320) Or _
                   (MarDirection = MA_Left And MarX <= 320) Then
                    'Start closing the doors as Mar gets half way across the screen
                    '(So that you don't have to wait too long)
                    If Status.CloseDoors(STATUS_SPEED_STANDARD) = True Then ModeStep = 2
                Else
                    'Make sure the doors are open (if the user presses enter before
                    'they've fully opened and the scene starts exiting)
                    Status.OpenDoors (STATUS_SPEED_STANDARD)
                End If
                'Move mar across the screen and eat the text
                If MarDirection = MA_Left Then MarX = (MarX - 9) Else MarX = (MarX + 9)
                
                'Animate Mar every other frame
                If (Frame.Count Mod 3) = 0 Then Sprites.Mar(MarDirection).Animate
                
            Case 2 '.................................................................
                'Once closed, what menu item was activated?
                Select Case SelectedMenu
                    Case PM_SELECTEDMENU.SM_StartGame
                        'Start a new game from fresh
                        Game.StartGame
                    
                    Case PM_SELECTEDMENU.SM_HowToPlay
                        'Start the How To Play screen
                        Set GameScreen = New IScreen_HowToPlay
                        'Tell the drawing and animating routines to handle the how _
                         to play screen
                        GameMode = GM_Title
                    
                    Case PM_SELECTEDMENU.SM_HighScores
                        'Start High scores screen
                        Project.StartHighScoreScreen True
                    
                    Case PM_SELECTEDMENU.SM_Quit
                        'End the game
                        Startup.ExitGame
                        Exit Sub
                        
                End Select
        End Select
    End If
    
    'Animate the ghosts
    For K = 0 To 3
        'Advance a frame every two rendered frames
        If (Frame.Count Mod 3) = 0 Then Sprites.Ghosts(K + 1, 0).Animate
        'Move the ghost in the right direction
        Dim Vec As PM_XY
        'Which corner does this ghost next stop at?
        Dim CornerTo As Long
        Select Case Ghosts(K).Dir
            Case DIR_RIGHT: CornerTo = 1
            Case DIR_DOWN: CornerTo = 2
            Case DIR_LEFT: CornerTo = 3
            Case DIR_UP: CornerTo = 0
        End Select
        'Create a vector for the Sprite's direction
        Vec = GetVectorFromDirection(Ghosts(K).Dir)
        Vec.X = Vec.X * 3: Vec.Y = Vec.Y * 3
        'Apply movement
        Ghosts(K).X = Range(Ghosts(K).X + Vec.X, Corners(0).X, Corners(1).X)
        Ghosts(K).Y = Range(Ghosts(K).Y + Vec.Y, Corners(0).Y, Corners(3).Y)
        'Has the ghost reached the corner?
        If (Ghosts(K).X = Corners(CornerTo).X) And (Ghosts(K).Y = Corners(CornerTo).Y) Then
            'Change direction
            Ghosts(K).Dir = 2 ^ ((CornerTo + 1) Mod 4)
        End If
    Next K
    
    'Scroll the background
    Background.Animate
End Sub

'====================================================================================
'ISCREEN Render: Draw the Title Screen
'====================================================================================
Private Sub IScreen_Render(ByVal hDC As Long)
    Dim X As Long, Y As Long
    Dim K As Long
    
    Dim cMarX As Long  'The horizontal centre point of Mar, not her left side
    Dim tLeft As Long
    
    'Draw the scrolling background on the main screen
    Background.Render hDC
    
    'Draw the main Pac Mar logo on the main screen
    GameMedia.DrawPacMarTitle hDC, 48, "~   A  d  v  e  n  t  u  r  e   ~", True
    
    'Draw the menu items
    For K = 0 To UBound(MenuItems)
        'If this is selected, draw on the cookies either end
        If (SelectedMenu = K) Then
            'Is the text being eaten by Mar?
            cMarX = (MarX + 36) 'center of Mar sprite
            
            'Draw on the text (selected)
            tLeft = Range(cMarX, MenuItems(K).X, MenuItems(K).X + MenuItems(K).Width) - MenuItems(K).X
            If MarDirection = MA_Right Then
                SharedAPI.Rectangle MenuItems(K).hDC(1), 0, 0, tLeft, 28
            ElseIf MarDirection = MA_Left Then
                SharedAPI.Rectangle MenuItems(K).hDC(1), tLeft, 0, MenuItems(K).Width, 28
            End If
            MenuItems(K).Draw hDC, True
            
            'Left hand cookie
            If (MarDirection = MA_Right And cMarX < MenuItems(K).X - 32) Or _
               (MarDirection = MA_Left And cMarX > MenuItems(K).X - 32) _
            Then
                Sprites.Cookie.DrawEx hDC, (MenuItems(K).X - 32), (MenuItems(K).Y + 6), 16, 16, 0, 0
            End If
            'Right hand cookie
            If (MarDirection = MA_Right And cMarX < (MenuItems(K).X + MenuItems(K).Width + 16)) Or _
               (MarDirection = MA_Left And cMarX > (MenuItems(K).X + MenuItems(K).Width + 16)) _
            Then
                Sprites.Cookie.DrawEx hDC, (MenuItems(K).X + MenuItems(K).Width + 16), (MenuItems(K).Y + 6), 16, 16, 16, 0
            End If
            
        Else
            'Draw on the text (unselected)
            MenuItems(K).Draw hDC, False
            
        End If
    Next K
    
    'Draw Pac Mar on the screen
    Sprites.Mar(MarDirection).Draw hDC, MarX, (MarY + 4)
    
    'Draw the ghosts on the screen
    For K = 0 To 3
        Sprites.Ghosts(K + 1, 0).Draw hDC, Ghosts(K).X - HalfTileSize, Ghosts(K).Y - HalfTileSize
    Next K
    
End Sub

'====================================================================================
'ISCREEN RenderTopBar: Render the top status bar display
'====================================================================================
Private Sub IScreen_RenderTopBar(ByVal hDC As Long)
    'This screen does not use the top status bar. This function has to be here in
    'order to comply with the IScreen Interface 'IScreen.cls'
End Sub

'====================================================================================
'ISCREEN RenderBottomBar: Render the bottom status bar display
'====================================================================================
Private Sub IScreen_RenderBottomBar(ByVal hDC As Long)
    'This screen does not use the bottom status bar. This function has to be here
    'in order to comply with the IScreen Interface 'IScreen.cls'
End Sub

'====================================================================================
'ISCREEN HandleKey: What to do when the user presses a key on this screen
'====================================================================================
Private Sub IScreen_HandleKey(ByVal KeyCode As Integer, ByVal Shift As Integer)
    'Ignore key presses when a menu selection has been made
    If ScreenMode = SM_Exit Then Exit Sub
    
    'If the user presses up / down keys, change the selection
    If KeyCode = vbKeyDown Then
        'Select the next menu (don't go off the bottom)
        SelectedMenu = Range(SelectedMenu + 1, 0, UBound(MenuItems))
        
    ElseIf KeyCode = vbKeyUp Then
        'Select the previous menu (don't go off the top)
        SelectedMenu = Range(SelectedMenu - 1, 0, UBound(MenuItems))
    
    ElseIf (KeyCode = 13 Or KeyCode = 32) Then
        'If the user presses Enter or space bar (and the title screen isn't closing
        'already, the user has selected a menu action
        '- Animate() above will decide what to do
        ScreenMode = SM_Exit
    
    ElseIf KeyCode = vbKeyEscape Then
        'If the user presses ESC, quit the game
        Startup.ExitGame
        
    End If
End Sub

'====================================================================================
'ISCREEN HandleMouseMove: What to do when the user hovers over the screen
'====================================================================================
Private Sub IScreen_HandleMouseMove(ByVal MouseX As Single, ByVal MouseY As Single)
    Dim K As Long
    
    'Is the mouse over a menu item?
    If (ScreenMode < SM_Exit) Then
        For K = 0 To UBound(MenuItems)
            If MenuItems(K).HitTest(MouseX, MouseY) = True Then
                'Select this menu item
                SelectedMenu = K
                Exit Sub
            End If
        Next K
    End If
End Sub

'====================================================================================
'ISCREEN HandleMouseClick: What to do when the user clicks the screen
'====================================================================================
Private Sub IScreen_HandleMouseClick(ByVal Button As Integer, ByVal Shift As Integer, ByVal MouseX As Integer, ByVal MouseY As Integer)
    Dim K As Long
    
    'Is the mouse clicked on menu item?
    If ScreenMode < SM_Exit Then
        For K = 0 To UBound(MenuItems)
            If MenuItems(K).HitTest(MouseX, MouseY) = True Then
                'Single left click?
                If Button = 1 Then
                    'Select that menu item. See Animate() above for details
                    ScreenMode = SM_Exit
                    Exit Sub
                End If
            End If
        Next K
    End If
End Sub

'====================================================================================
'CLASS Terminate - When the TitleScreen is unloaded, free up memory
'====================================================================================
Private Sub Class_Terminate()
    Set Background = Nothing
    Erase MenuItems
End Sub

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
